package uk.ac.surrey.agupta.acquisition;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;

/**
 * @author Ashish Gupta
 * CVSSP, University of Surrey
 * Copyright (c) 2010 Ashish Gupta
 * All rights reserved.
 */
/**
 * 
 */

/**
 * @author Ashish
 *
 */
public class ReadSIFTFile{

	/**
	 * @param args
	 * @throws IOException 
	 */
	public static void main(String[] args) throws IOException {
//		File file = new File(args[0]);
		File file = new File("c:/Users/Ashish/Jworkspace/objectcategoryclassification/boilerplate/uk/ac/surrey/agupta/acquisition/ReadSIFTDataArgFile.txt");
		FileReader fr = new FileReader(file);
		BufferedReader br = new BufferedReader(fr);
		String imageSetFileName = br.readLine();
		String keypointFolderName = br.readLine();
		File imageSetFile = new File(imageSetFileName);
		FileReader imageSetFileReader = new FileReader(imageSetFile);
		BufferedReader imageSetBufferedReader = new BufferedReader(imageSetFileReader);
		ArrayList<String> imageName = new ArrayList<String>();
		ArrayList<Integer> imageType = new ArrayList<Integer>();
		String line = null;
		while((line=imageSetBufferedReader.readLine())!= null){
			String[] inLine = line.split(" ");
			imageName.add(inLine[0]);
			imageType.add(Integer.parseInt(inLine[1]));
		}
		String keypointFileName = keypointFolderName+imageName.get(0)+".key";
		FileReader keypointFileReader = new FileReader(keypointFileName);
		BufferedReader keypointBufferedReader = new BufferedReader(keypointFileReader);
		String[] keypointspecs = keypointBufferedReader.readLine().split(" ");
		int numKeypoint = Integer.parseInt(keypointspecs[0]);
		int numDescriptorDimensions = Integer.parseInt(keypointspecs[1]);
		int imgFV[][] = new int[numKeypoint][numDescriptorDimensions];
		for(int i=0;i<numKeypoint;i++){
			line = keypointBufferedReader.readLine();
			for(int j=0;j<numDescriptorDimensions;j++){
				imgFV[i][j] = keypointBufferedReader.read();
			}
		}
		String imgFVFileName = "imageFeatureVectors.txt";
		File imgFVfile = new File(imgFVFileName);
		FileWriter imgFVfileWriter = new FileWriter(imgFVfile);
		BufferedWriter imgFVBufferedWriter = new BufferedWriter(imgFVfileWriter);
		for(int i=0;i<numKeypoint;i++){
			for(int j=0;j<numDescriptorDimensions;j++){
				imgFVBufferedWriter.write(imgFV[i][j] + ",");
			}
			imgFVBufferedWriter.write("\n");
		}
	}

}
